// ==================================================================
// 描 述：
// 作 者：王征:D
// 创建时间：2019/05/20 14:16:44
// ==============================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;

namespace AutoChess
{
    public class AutoChessRewardsPanel : MonoBehaviour
    {
        public AutoChessRewardCell Prefab;
        public AutoChessRewardsTitleCell prefabTitle;
        public Transform Content;
        public GameObject[] TypeBtn;
        public Text WeeklyTimes;
        public Text endTime;
        private List<AutoChessRewardCell> allCells;
        private List<AutoChessRewardsTitleCell> allTitleCells;
        private bool inited;

        private void Init()
        {
            if (inited)
                return;
            inited = true;

            InitCell();
            InitTitleCell();

            SetDisplayType(1);
            var date = AutoChessManager.Ins.cacheData.SeasonEndTime;
            endTime.text = date.ToString(TableTextBlo.GetText("ChessRewardEndDesc"));
        }

        private void InitCell()
        {
            allCells = new List<AutoChessRewardCell>();
           
            var allGrades = Table.Blo.AutoChessBlo.GetAllGrades();
            List<List<Table.DataModel.TableAutoChessGrade>> grades = new List<List<Table.DataModel.TableAutoChessGrade>>();
            for (int i = 0; i < allGrades.Count; i++)
            {
                var rankType = allGrades[i].rankType;
                if (grades.Count < rankType)
                {
                    grades.Add(new List<Table.DataModel.TableAutoChessGrade>());
                    var cell = Instantiate<AutoChessRewardCell>(Prefab, Content);
                    cell.gameObject.SetActive(true);
                    allCells.Add(cell);
                    cell.gameObject.transform.SetAsFirstSibling();
                }
                grades[rankType - 1].Add(allGrades[i]);
            }
            for (int i = 0; i < grades.Count; i++)
            {
                allCells[i].SetData(grades[i]);
            }

            SetWeeklyReward();
        }

        private void InitTitleCell()
        {
            var allTitles = Table.Blo.AutoChessBlo.GetAllSeasionTitle();
            allTitleCells = new List<AutoChessRewardsTitleCell>();
            int index = 0;
            foreach (var item in allTitles)
            {
                var cell = Instantiate<AutoChessRewardsTitleCell>(prefabTitle, Content);
                cell.Init(item);
                cell.gameObject.SetActive(true);
                allTitleCells.Add(cell);
            }
        }

        public void Refresh()
        {
            Init();
            //WeeklyTimes.text = AutoChessManager.Ins.cacheData.WeekFightCount.ToString();
        }

        private void SetWeeklyReward()
        {
            foreach (var item in allCells)
            {
                item.SetDisplayType(1);
            }
        }

        public void SetDisplayType(int type)
        {
            TypeBtn[0].SetActive(type == 0);
            TypeBtn[1].SetActive(type == 1);
            if (type == 0)
            {
                SetAllCellsState(false);
                SetAllCellsTitleState(true);
            }
            else
            {
                SetAllCellsState(true);
                SetAllCellsTitleState(false);
            }
        }

        private void SetAllCellsState(bool active)
        {
            foreach (var item in allCells)
            {
                item.gameObject.SetActive(active);
            }
        }

        private void SetAllCellsTitleState(bool active)
        {
            foreach (var item in allTitleCells)
            {
                item.gameObject.SetActive(active);
            }
        }

        public void Close()
        {
            gameObject.SetActive(false);
        }
    }
}
